package com.bms.hystrix.service.zl;

import com.bms.hystrix.interfaces.zl.ServiceHystrix;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class CheekService {

    @Resource
    private ServiceHystrix serviceHystrix;

    // 通过feign调用服务模块，如果触发熔断机制，则会调用此注解checkError定义的方法
    // 熔断器会检查所有的client是否存在，如果都不存在，则会触发HystrixCommand
    @HystrixCommand(fallbackMethod = "checkError")
    public Map check(String username) {
        return serviceHystrix.selectClient(username);
    }

    public Map checkError(String username) {
        Map map = new HashMap();
        map.put("code", 500);
        map.put("msg","服务异常，请稍后重试！");
        map.put("username", username);
        return map;
    }
}
